package top.kuxiao.crmserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.kuxiao.crmserver.pojo.*;
import top.kuxiao.crmserver.service.IAccountManagerService;
import top.kuxiao.crmserver.service.IDevPlanService;
import top.kuxiao.crmserver.service.ISaleChanceService;
import top.kuxiao.crmserver.service.ISysUserService;
import top.kuxiao.crmserver.utils.Auth;
import top.kuxiao.crmserver.utils.CookieUtil;
import top.kuxiao.crmserver.utils.ResultUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 客户开发计划 前端控制器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-13
 */
@Api(tags = "客户开发计划")
@CrossOrigin
@RestController
@RequestMapping("/dev-plan")
public class DevPlanController {

    @Autowired
    ISaleChanceService saleChanceService;

    @Autowired
    IDevPlanService devPlanService;

    @Autowired
    IAccountManagerService accountManagerService;

    AccountManager accountManager;

    @Autowired
    CookieUtil cookieUtil;


    @ApiOperation(value = "查询一个")
    @ApiImplicitParam(name = "id", value = "计划的id")
    @RequestMapping(value = "one/{id}", method = RequestMethod.POST)
    public ResultUtil findOne(@PathVariable("id") Integer id) {
        DevPlan byId = devPlanService.getById(id);
        return ResultUtil.resObj(byId);
    }

    /**
     * 已经制定的计划
     */
    @Auth("AM")
    @ApiImplicitParam(name = "id", value = "机会id")
    @ApiOperation(value = "已经制定的计划", notes = "客户经理访问，显示已经制定的计划")
    @RequestMapping(value = "plan/{id}", method = RequestMethod.GET)
    public ResultUtil infoPlan(@PathVariable("id") Integer id) {

        QueryWrapper<DevPlan> devPlanQueryWrapper = new QueryWrapper<>();

        devPlanQueryWrapper.eq("dp_sc_id", id);

        List<DevPlan> list = devPlanService.list(devPlanQueryWrapper);
        return ResultUtil.resObj(list);
    }


    /**
     * 显示该账号已经分配到的机会的全部信息
     */
    @Auth("AM")
    @ApiOperation(value = "机会的全部信息", notes = "客户经理访问，制定开发计划时显示")
    @RequestMapping(value = "chance", method = RequestMethod.GET)
    public ResultUtil infoChance(HttpServletRequest request) {


        IUser object = cookieUtil.getObject(request);
        if (object instanceof AccountManager) {
            accountManager = (AccountManager) object;
        }

        //获取全部机会的分配
        List<SaleChance> saleChances = saleChanceService.queryAll();

        List<SaleChance> list = new ArrayList<>();


        //查询当前账号被分配到的机会
        for (SaleChance saleChance : saleChances) {
            if (Objects.equals(saleChance.getScAssignToId(), accountManager.getId())) {
                list.add(saleChance);
            }
        }

        return ResultUtil.resObj(list);
    }


    /**
     * 开发成功
     */
    @Auth("AM")
    @ApiImplicitParam(name = "scId", value = "机会的id")
    @ApiOperation(value = "开发成功", notes = "成功以后跳转到创建客户的请求")
    @RequestMapping(value = "succeed", method = RequestMethod.PUT)
    public ResultUtil succeed(Integer scId) {

        SaleChance byId = saleChanceService.getById(scId);
        byId.setOppStateId(1003);


        if (saleChanceService.updateById(byId)) {


            return ResultUtil.resMsgOK("开发成功");
        } else {
            return ResultUtil.resMsgError("开发失败");
        }

    }


    /**
     * 记录执行结果
     */
    @Auth("AM")
    @ApiOperation(value = "更新执行结果")
    @RequestMapping(value = "results", method = RequestMethod.PUT)
    public ResultUtil update(DevPlan plan) {
        Integer dpScId = plan.getDpScId();

        SaleChance byId = saleChanceService.getById(dpScId);

        if (1002!=byId.getOppStateId()){
            return ResultUtil.resMsgError("失败");
        }

        plan.setDpExecutionDate(new Date());

        if (devPlanService.updateById(plan)) {
            return ResultUtil.resMsgOK("成功");
        }
        return ResultUtil.resMsgError("失败");
    }


    /**
     * 添加机会时显示该机会的全部信息
     */
    @ApiImplicitParam(name = "id", value = "机会的id")
    @ApiOperation(value = "显示要添加计划的机会信息", notes = "在制定计划前执行")
    @RequestMapping(value = "show/{id}", method = RequestMethod.POST)
    public ResultUtil show(@PathVariable("id") Integer id) {

        SaleChance byId = saleChanceService.getById(id);

        if (byId != null) {
            return ResultUtil.resObj(byId);
        }

        return ResultUtil.resMsgError("");
    }


    /**
     * 制定开发计划
     */
    @Auth("AM")
    @ApiOperation(value = "添加计划", notes = "客户经理访问，添加一个计划")
    @RequestMapping(value = "plan/{id}", method = RequestMethod.POST)
    public ResultUtil addPlan(
            DevPlan plan,
            @PathVariable("id") Integer id) {

        SaleChance byId = saleChanceService.getById(id);

        plan.setDpScId(byId.getScId());

        plan.setDpExecutorId(accountManager.getId());


        return devPlanService.save(plan) ? ResultUtil.resMsgOK("") : ResultUtil.resMsgError("");

    }


    /**
     * 终止开发按钮
     */
    @ApiImplicitParam(name = "scId", value = "机会的id")
    @ApiOperation(value = "终止开发按钮")
    @RequestMapping(value = "stop", method = RequestMethod.PUT)
    public ResultUtil stop(Integer scId) {


        SaleChance byId = saleChanceService.getById(scId);
        byId.setOppStateId(1004);


        if (saleChanceService.updateById(byId)) {

            return ResultUtil.resMsgOK("终止成功");
        } else {
            return ResultUtil.resMsgError("终止失败");
        }

    }

}
